package com.growthgate.bootcommon.controller;

import com.growthgate.bootcommon.amqp.sender.DirectSender;
import com.growthgate.bootcommon.amqp.sender.FanoutSender;
import com.growthgate.bootcommon.amqp.sender.TopicSender;
import com.growthgate.bootcommon.common.base.BaseController;
import com.growthgate.bootcommon.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * RabbitMqController
 *
 * @author GrowthGate
 * @date 2019/10/28
 */
@Api(tags = "RabbitMq")
@RestController
@RequestMapping("/amqp")
public class RabbitMqController extends BaseController {

    @Resource
    private FanoutSender fanoutSender;

    @Resource
    private DirectSender directSender;

    @Resource
    private TopicSender topicSender;

    /**
     * receiveTopic1和receiveTopic2都收到消息
     *
     * @param user 用户
     * @return 用户
     */
    @PutMapping("/send/fanout")
    @ApiOperation("测试fanout")
    public User sendFanout(@ApiParam("测试用用户对象") @RequestBody User user) {
        fanoutSender.send(user);
        return user;
    }

    /**
     * receiveDirect1和receiveDirect2轮流收到消息
     *
     * @param user 用户
     * @return 用户
     */
    @PutMapping("/send/direct")
    @ApiOperation("测试direct")
    public User sendDirect(@ApiParam("测试用用户对象") @RequestBody User user) {
        directSender.send(user);
        return user;
    }

    /**
     * receiveTopic1收到一次消息，receiveTopic2收到两次消息
     *
     * @param user 用户
     * @return 用户
     */
    @PutMapping("/send/topic")
    @ApiOperation("测试topic")
    public User sendTopic(@ApiParam("测试用用户对象") @RequestBody User user) {
        topicSender.send(user);
        return user;
    }

}